package cac.fenjian.resource.palletCodeQuery.rest;

import cac.fenjian.resource.MsWorkbinBasic.dto.MsWorkbinBasicDTO;
import cac.fenjian.resource.palletCodeQuery.service.PalletCodeQueryService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/PalletCode")
public class PalletCodeQueryRest {
    @Autowired
    private PalletCodeQueryService palletCodeQueryService;
    /*托盘信息查询*/
    @PostMapping("/palletCodeQuery")
    public ResponseMsg<QueryRespBean<HashMap>> palletCodeQuery(@RequestBody HashMap map ){
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), MsWorkbinBasicDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            HashMap searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<HashMap>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<HashMap> result = palletCodeQueryService.palletCodeQuery(queryReqBean, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        List<HashMap> result1 = palletCodeQueryService.palletCodeQuery(searchParams, queryReqBean.getSortExp());
        if(result1.size()!=0){
            responseMsg.setResponseBody(result).setRetCode("200");
        }
        else responseMsg.setRetCode("201").setMessage("未查询到数据").setResponseBody(result);
        responseMsg.setResponseBody(result);
        return  responseMsg;

    }
}
